Vue.prototype.$http = axios; // 全局配置
axios.defaults.baseURL="http://localhost:8080" // 配置基础路径
//======================axios的前置拦截器【拦截调用后端的请求====================//
axios.interceptors.request.use(res=>{
    let token = localStorage.getItem("token");
    if(token){
        res.headers["token"] = token;
    }
    return res;
},error => {
    Promise.reject(error)
})
//======================axios的前置拦截器【拦截调用后端的请求】====================//


//======================axios的后置拦截器【处理后台登录拦截的结果】====================//
axios.interceptors.response.use(res => {
    //后端响应的是没有登录的信息
    if (false === res.data.success && "toLogin" === res.data.message) {
        localStorage.removeItem("token");
        localStorage.removeItem("username");
        location.href="/login.html"
    }
    return res;
},error => {
    Promise.reject(error)
})
//======================axios的后置拦截器【处理后台登录拦截的结果】====================//
// 其他页面的拦截
let url = location.href
if (url.indexOf("login.html") == -1 && url.indexOf("register.html") == -1  && url.indexOf("binder.html") == -1
    && url.indexOf("callback.html") == -1){
    let username = localStorage.getItem("username")
    if (!username){
        location.href="/login.html"
    }
}

// http://bugtracker.itsource.cn/callback.html?code=061PMc000tueiQ132B300rWl400PMc0W&state=1
// 对地址栏参数获取方法
function parseUrlParams2Obj(url){
    let params = url.substring(url.indexOf("?") + 1) // code=061PMc000tueiQ132B300rWl400PMc0W&state=1
    let arr = params.split("&") // code=061PMc000tueiQ132B300rWl400PMc0W 1  state=1 2
    var obj = {} // 空对象
    for (var i = 0; i < arr.length; i++ ){
        let data = arr[i]  // key=value
        let key = data.split("=")[0]  // code
        let value = data.split("=")[1] // 061PMc000tueiQ132B300rWl400PMc0W
        obj[key] = value //  code=061PMc000tueiQ132B300rWl400PMc0W
    }
    return obj;
}